package net.oschina.app.util;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;

import net.oschina.app.emoji.DisplayRules;
import net.oschina.app.emoji.Emojicon;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmoParser {
    public static SpannableStringBuilder parserEmo(Context context, String content) {
        //[x1f600]
        SpannableStringBuilder builder = new SpannableStringBuilder(content);
        //正则表达式
        Pattern pattern = Pattern.compile("\\[(.*?)\\]");
        Matcher matcher = pattern.matcher(content);
        int startIndex = 0;
        int index = 0;
        while (matcher.find()) {
            String emo = matcher.group();//[x1f600]
            emo = emo.substring(1, emo.length() - 1);//x1f600
            //获取表情的id
            int id = context.getResources().getIdentifier(string2sourse(emo), "drawable", context.getPackageName());
            // 1.获取表情的图片
            Drawable drawable = context.getResources().getDrawable(id);
            // 用中括号包一下，方便替换
            //emoName = "[" + emoName + "]";
            // 可实现图文混排的类
            //SpannableStringBuilder builder = new SpannableStringBuilder(emoName);
            int size = (int) (25 * context.getResources().getDisplayMetrics().density);
            // 必须设置图片的大小
            drawable.setBounds(0, 0, size, size);
            // 包装一下图片
            ImageSpan span = new ImageSpan(drawable);
            // 把文字替换成图片
            startIndex = content.indexOf("[", startIndex + index);
            builder.setSpan(span, startIndex, startIndex + 4,
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            index = 1;
        }
        return builder;
    }

    public static SpannableStringBuilder parserEmoji(Context context, String content) {
        SpannableStringBuilder builder = new SpannableStringBuilder(content);
        //正则表达式
        Pattern pattern = Pattern.compile("\\[(.*?)\\]");
        Matcher matcher = pattern.matcher(content);
        int startIndex = 0;
        int emoLength = 0;
        int cutLength = 0;
        //因为可能会有重复的图片,所以裁剪
        String copyContent = content;
        while (matcher.find()) {
            String emo = matcher.group();//[微笑]
            startIndex = copyContent.indexOf(emo);
            emoLength = emo.length();
            //获取id
            Emojicon emojiFromName = DisplayRules.getEmojiFromName(emo);
            int resId = emojiFromName.getResId();
            // 1.获取表情的图片
            Drawable drawable = context.getResources().getDrawable(resId);
            int size = (int) (25 * context.getResources().getDisplayMetrics().density);
            // 必须设置图片的大小
            drawable.setBounds(0, 0, size, size);
            // 包装一下图片
            ImageSpan span = new ImageSpan(drawable);
            // 把文字替换成图片
            builder.setSpan(span, startIndex + cutLength, startIndex + cutLength + emoLength,
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            cutLength = cutLength + startIndex + emoLength;
            copyContent = content.substring(cutLength);
        }
        return builder;
    }

    private static String string2sourse(String emo) {
        String string[] = {"调皮", "呲牙", "得意"};
        String sourse[] = {"smiley_12", "smiley_13", "smiley_4"};
        for (int i = 0; i < string.length; i++) {
            if (string[i].equals(emo)) {
                return sourse[i];
            }
        }
        return "";
    }


    public static String richText(String title, String body) {
        String source = g(title) + ": " + body;
        return source;
    }

    //取红颜色
    //<font color='#ff0000'>谷歌</font>
    public static String r(String source) {
        //对字符串设置格式  ，%s：占位符
        return String.format("<font color='#FF0000'>%s</font>", source);
    }

    //取颜色
    public static String g(String source) {
        return String.format("<font color='#00EE00'>%s</font>", source);
    }

    //取链接
    //  <a href="http://www.baidu.com">百度</a>
    // linkText: 百度    linkUrl：http://www.baidu.com
    public static String getlink(String linkText, String linkurl) {
        // sql ? 占位符
        return String.format("<a href=\"%s\">%s</a>", linkurl, linkText);
    }
}
